package com.ihg.hiex.admin.web.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.entity.result.HttpResult;
import com.demo2do.core.web.interceptor.MenuSetting;
import com.demo2do.core.web.resolver.Preloading;
import com.ihg.hiex.admin.entity.security.Login;
import com.ihg.hiex.admin.service.account.AccountService;
import com.ihg.hiex.admin.service.account.AdministratorService;
import com.ihg.hiex.admin.service.account.SupervisorService;
import com.ihg.hiex.admin.web.resolver.Secure;
import com.ihg.hiex.entity.user.Account;
import com.ihg.hiex.entity.user.Administrator;
import com.ihg.hiex.entity.user.Supervisor;

/**
 * 个人中心
 * 
 * @author Ysy
 *
 */
@Controller
@RequestMapping("/profile")
@MenuSetting("menu-profile")
public class ProfileController {
    
    private static final Logger logger = LoggerFactory.getLogger(ProfileController.class);
    
    @Autowired
    private AdministratorService administratorService;
    
    @Autowired
    private SupervisorService supervisorService;
    
    @Autowired
    private AccountService accountService;

    /**
     * 个人中心 - 首页
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "" , method = RequestMethod.GET)
    public ModelAndView index(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("system/profile/profile-" + login.getAuthorityAlias());
        
        if(login.isAdministrator()) {
            
            Administrator administrator = administratorService.load(Administrator.class, login.getId());
            modelAndView.addObject("administrator", administrator);
            
        } else if(login.isSupervisor()) {
            
            Supervisor supervisor = supervisorService.load(Supervisor.class, login.getId());
            modelAndView.addObject("supervisor", supervisor);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * edit password panel
     * 
     * @return
     */
    @RequestMapping(value = "/edit-password" , method = RequestMethod.GET)
    public String editPassword() {
        return "system/profile/profile-edit-password";
    }
    
    /**
     * on edit password
     * 
     * @param login
     * @param oldPassword
     * @param newPassword
     * @return
     */
    @RequestMapping(value = "/edit-password" , method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public @ResponseBody HttpResult onEditPassword(@Secure Login login,
                                                   @RequestParam("oldPassword") String oldPassword,
                                                   @RequestParam("newPassword") String newPassword) {
        
        HttpResult httpResult = new HttpResult();

        try {
            
            if (login.isAdministrator()) {
                
                Administrator administrator = administratorService.load(Administrator.class, login.getId());
                Account account = administrator.getAccount();
                
                accountService.editPassword(account, oldPassword, newPassword);
                
            } else if (login.isSupervisor()) {
                
                Supervisor supervisor = supervisorService.load(Supervisor.class, login.getId());
                Account account = supervisor.getAccount();
                
                accountService.editPassword(account, oldPassword, newPassword);
                
            }
            
            httpResult.success().message("Submit Successful");
        
        } catch(Exception e) {
            logger.error("#onEditPassword() - on edit password fail, exception=[{}]", e);
        	httpResult.success().message("failed! " + e.getMessage());
        }
        
        return httpResult;
        
    }
    

    /**
     * edit info panel
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "/edit", method = RequestMethod.GET)
    public ModelAndView edit(@Secure Login login) {

        ModelAndView modelAndView = new ModelAndView("system/profile/profile-" + login.getAuthorityAlias() + "-edit");
        
        if (login.isAdministrator()) {
            
            Administrator administrator = administratorService.load(Administrator.class, login.getId());
            modelAndView.addObject("administrator", administrator);
            
        } else if (login.isSupervisor()) {
            
            Supervisor supervisor = supervisorService.load(Supervisor.class, login.getId());
            modelAndView.addObject("supervisor", supervisor);
            
        }
        
        return modelAndView;
        
    }
    
    /**
     * on edit admin info
     * 
     * @param login
     * @param administrator
     * @return
     */
    @RequestMapping(value = "/edit" , method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = {"role=administrator"})
    public @ResponseBody HttpResult onEdit(@Secure Login login, @Preloading Administrator administrator) {
        
        HttpResult httpResult = new HttpResult();

        try {
            
            administratorService.edit(administrator);
            
        	httpResult.success().message("Submit Successful");
        
        } catch(Exception e) {
            logger.error("#onEdit() - edit info failed, exception=[{}]", e);
        	httpResult.success().message("failed");
        }
        
        return httpResult;
        
    }
    
    /**
     * on edit supervisor info
     * 
     * @param login
     * @param supervisor
     * @return
     */
    @RequestMapping(value = "/edit" , method = RequestMethod.PATCH, produces = MediaType.APPLICATION_JSON_UTF8_VALUE, params = {"role=supervisor"})
    public @ResponseBody HttpResult onEdit(@Secure Login login, @Preloading Supervisor supervisor) {
        
        HttpResult httpResult = new HttpResult();

        try {
            
            supervisorService.edit(supervisor);
            
            httpResult.success().message("Submit Successful");
        
        } catch(Exception e) {
            logger.error("#onEdit() - edit info failed, exception=[{}]", e);
            httpResult.success().message("failed");
        }
        
        return httpResult;
        
    }
    
}